package models

import (
	"gitchina/kly_service/data"
	"gitchina/kly_service/util"
	"strconv"
)

const (
	REGISTION_POINT           = 50
	REGISTION_POINT_TYPE      = 1
	AUTH_COMPLETE_POINT       = 50
	AUTH_COMPLETE_POINT_TYPE  = 2
	ORDER_COMPLETE_POINT      = 100
	ORDER_COMPLETE_POINT_TYPE = 3
	RECOMMENDATION_POINT      = 20
	RECOMMENDATION_POINT_TYPE = 4

	POINT_EXTRA_INFO = "*"
)

type PointAdd struct {
	MemberID ID     `json:"member_id"`
	Rand     string `json:"rand"`
	Token    string `json:"token"`
}

func AddPointForRegistion(memberID int) {
	var repo data.MemberRepo
	if err := repo.AddPoint(data.PointDto{
		MemberID: memberID,
		Point:    REGISTION_POINT,
		Type:     REGISTION_POINT_TYPE,
		Extra:    POINT_EXTRA_INFO,
	}); err != nil {
		util.GetLogger().Error("[model.AddPointForRegistion] - add point error: %s", err.Error())
	}
}

func AddPointForAuthCompletion(memberID ID) {
	var repo data.MemberRepo
	if _, _, _, mid, err := memberID.Decode(); err == nil {
		if err := repo.AddPoint(data.PointDto{
			MemberID: int(mid),
			Point:    AUTH_COMPLETE_POINT,
			Type:     AUTH_COMPLETE_POINT_TYPE,
			Extra:    POINT_EXTRA_INFO,
		}); err != nil {
			util.GetLogger().Error("[model.AddPointForAuthComplete] - add point error: %s", err.Error())
		}
	}
}

func AddPointForRecommendation(memberID ID) {
	var repo data.MemberRepo
	if _, _, _, mid, err := memberID.Decode(); err == nil {
		if err := repo.AddPoint(data.PointDto{
			MemberID: int(mid),
			Point:    RECOMMENDATION_POINT,
			Type:     RECOMMENDATION_POINT_TYPE,
			Extra:    POINT_EXTRA_INFO,
		}); err != nil {
			util.GetLogger().Error("[model.AddPointForRecommendation] - add point error: %s", err.Error())
		}
	}
}

func AddPointForOrderCompletion(memberID, waybillID int) {
	var repo data.MemberRepo
	if err := repo.AddPoint(data.PointDto{
		MemberID: memberID,
		Point:    ORDER_COMPLETE_POINT,
		Type:     ORDER_COMPLETE_POINT_TYPE,
		Extra:    strconv.Itoa(waybillID),
	}); err != nil {
		util.GetLogger().Error("[model.AddPointForOrderCompletion] - add point error: %s", err.Error())
	}
}
